/**********************************************************************************************************
** 驱动文件：	ups-KR3XXX.c
** 驱动类型：	ups
** 设备名称：	科华UPS
** 设备型号：	KR3XXX
** 设备品牌：	科华
** 设备协议：	科华三相串口协议
** 驱动描述：	适用于科华三相UPS系列产品。
** 生成日期：	2018-04-03 09:36:37
**********************************************************************************************************/
#include "Includes.h"


/**********************************************************************************************************
* 定义设备数据点变量结构
**********************************************************************************************************/
typedef struct{
	Var batVolt;								//电池电压
	Var batCapacity;							//电池容量
	Var batRemainingTime;						//电池剩余供电时间
	Var batCurrent;								//电池电流
	Var temp;									//UPS温度
	Var inputFreq;								//输入频率
	Var bypassFreq;								//旁路频率
	Var outputFreq;								//输出频率
	Var rectifierState;							//整流器状态
	Var batLowVoltProtect;						//电池欠压保护
	Var batLowVolt;								//电池欠压
	Var inputOutputType;						//输入输出类型
	Var runModel;								//运行模式
	Var chargeState;							//电池充电状态
	Var rectifierRunning;						//整流器运行
	Var bypassFreqState;						//旁路频率状态
	Var manualBypassState;						//手动旁路
	Var bypassACState;							//旁路交流
	Var staticSwitchState;						//静态开关模式
	Var inverterRunning;						//逆变器运行
	Var emergencyStop;							//紧急关机
	Var DCInputHighStop;						//直流输入过高关机
	Var manualBypassClose;						//手动旁路闭合关机
	Var overload;								//过载关机
	Var outputVoltAbnormal;						//输出电压异常关机
	Var overTemp;								//过温关机
	Var outputShortCircuit;						//输出短路关机
	Var inputUA;								//A相输入电压
	Var inputUB;								//B相输入电压
	Var inputUC;								//C相输入电压
	Var bypassUA;								//A相旁路电压
	Var bypassUB;								//B相旁路电压
	Var bypassUC;								//C相旁路电压
	Var outputUA;								//A相输出电压
	Var outputUB;								//B相输出电压
	Var outputUC;								//C相输出电压
	Var loadPercentageA;						//A相负载率
	Var loadPercentageB;						//B相负载率
	Var loadPercentageC;						//C相负载率
}Data_Struct;									//定义变量结构


/**********************************************************************************************************
* 设备数据点变量属性声明
**********************************************************************************************************/
const static Var_Property_Def var_attr[] = {
	{
		INTEGER,
		0,
		READONLY,
		"batVolt",
		"电池电压",
		"V",
		"电池电压",
		0,
		0,
		"电池电压过高",
		"电池电压过低",
		0,
		0
	},
	{
		INTEGER,
		0,
		READONLY,
		"batCapacity",
		"电池容量",
		"%",
		"电池容量",
		0,
		0,
		"电池容量不足",
		"电池容量充足",
		0,
		0
	},
	{
		INTEGER,
		0,
		READONLY,
		"batRemainingTime",
		"电池剩余供电时间",
		"分钟",
		"电池剩余供电时间",
		0,
		0,
		"电池备份时间不足",
		"电池备份时间充足",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"batCurrent",
		"电池电流",
		"A",
		"电池充/放电电流",
		0,
		0,
		"电池电流过高",
		"电池电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"temp",
		"UPS温度",
		"℃",
		"UPS温度",
		0,
		0,
		"UPS温度过高",
		"UPS温度过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"inputFreq",
		"输入频率",
		"Hz",
		"输入频率",
		0,
		0,
		"输入频率过高",
		"输入频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"bypassFreq",
		"旁路频率",
		"Hz",
		"旁路频率",
		0,
		0,
		"旁路频率过高",
		"旁路频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"outputFreq",
		"输出频率",
		"Hz",
		"输出频率",
		0,
		0,
		"输出频率过高",
		"输出频率过低",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"rectifierState",
		"整流器状态",
		"",
		"整流器状态：0-正常，1-异常",
		0,
		0,
		"整流器异常",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"batLowVoltProtect",
		"电池欠压保护",
		"",
		"电池欠压保护：0-无，1-欠压保护",
		0,
		0,
		"欠压保护",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"batLowVolt",
		"电池欠压",
		"",
		"电池欠压：0-无，1-欠压",
		0,
		0,
		"电池欠压",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"inputOutputType",
		"输入输出类型",
		"",
		"输入输出类型：0-三进三出，1-三进单出",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"runModel",
		"运行模式",
		"",
		"运行方式：0-电池充电，市电正常，在线运行，1-电池放电，后备式运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"chargeState",
		"电池充电状态",
		"",
		"电池充电状态：0-浮充，1-均充",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"rectifierRunning",
		"整流器运行",
		"",
		"整流器运行：0-关闭，1-开启",
		0,
		0,
		"关闭",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"bypassFreqState",
		"旁路频率状态",
		"",
		"旁路频率状态：0-正常，1-异常",
		0,
		0,
		"旁路频率异常",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"manualBypassState",
		"手动旁路",
		"",
		"手动旁路状态：0-断开，1-闭合",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"bypassACState",
		"旁路交流",
		"",
		"旁路交流电状态：0-异常，1-正常",
		0,
		0,
		"旁路交流电异常",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"staticSwitchMode",
		"静态开关模式",
		"",
		"静态开关状态：0-旁路模式，1-逆变模式",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"inverterRunning",
		"逆变器运行",
		"",
		"逆变器运行：0-关闭，1-开启",
		0,
		0,
		"逆变器运行停止",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"emergencyStop",
		"紧急关机",
		"",
		"紧急关机：0-无，1-发生",
		0,
		0,
		"UPS紧急关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"DCInputHighStop",
		"直流输入过高关机",
		"",
		"直流输入过高关机：0-无，1-发生",
		0,
		0,
		"直流输入过高关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"manualBypassClose",
		"手动旁路闭合关机",
		"V",
		"手动旁路闭合关机：0-无，1-发生",
		0,
		0,
		"手动旁路闭合关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"overload",
		"过载关机",
		"",
		"过载关机：0-无，1-发生",
		0,
		0,
		"过载关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"outputVoltAbnormal",
		"输出电压异常关机",
		"",
		"输出电压异常关机：0-无，1-发生",
		0,
		0,
		"输出电压异常关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"overTemp",
		"过温关机",
		"",
		"过温关机：0-无，1-发生",
		0,
		0,
		"过温关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"outputShortCircuit",
		"输出短路关机",
		"",
		"输出短路关机：0-无，1-发生",
		0,
		0,
		"输出短路关机",
		"",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"inputUA",
		"A相输入电压",
		"V",
		"A相输入电压",
		0,
		0,
		"A相输入电压过高",
		"A相输入电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"inputUB",
		"B相输入电压",
		"V",
		"B相输入电压",
		0,
		0,
		"B相输入电压过高",
		"B相输入电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"inputUC",
		"C相输入电压",
		"V",
		"C相输入电压",
		0,
		0,
		"C相输入电压过高",
		"C相输入电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"bypassUA",
		"A相旁路电压",
		"V",
		"A相旁路电压",
		0,
		0,
		"A相旁路电压过高",
		"A相旁路电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"bypassUB",
		"B相旁路电压",
		"V",
		"B相旁路电压",
		0,
		0,
		"B相旁路电压过高",
		"B相旁路电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"bypassUC",
		"C相旁路电压",
		"V",
		"C相旁路电压",
		0,
		0,
		"C相旁路电压过高",
		"C相旁路电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"outputUA",
		"A相输出电压",
		"V",
		"A相输出电压",
		0,
		0,
		"A相输出电压过高",
		"A相输出电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"outputUB",
		"B相输出电压",
		"V",
		"B相输出电压",
		0,
		0,
		"B相输出电压过高",
		"B相输出电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"outputUC",
		"C相输出电压",
		"V",
		"C相输出电压",
		0,
		0,
		"C相输出电压过高",
		"C相输出电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"loadPercentageA",
		"A相负载率",
		"%",
		"A相输出负载率",
		0,
		0,
		"A相输出负载率过高",
		"A相输出负载率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"loadPercentageB",
		"B相负载率",
		"%",
		"B相输出负载率",
		0,
		0,
		"B相输出负载率过高",
		"B相输出负载率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"loadPercentageC",
		"C相负载率",
		"%",
		"C相输出负载率",
		0,
		0,
		"C相输出负载率过高",
		"C相输出负载率过低",
		0,
		0
	}
};


/**********************************************************************************************************
* 函数名称： static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
* 函数说明： 生成获取传感器数据指令
* 输入参数： 指令索引，传感器地址，变量结构体指针,以及指令缓存区指针,指令长度
* 返回参数： 无
**********************************************************************************************************/
static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
{
	switch(index)
	{
		case 0:
			*len = sprintf(cmd,"G1\r");
			break;
		case 1:
			*len = sprintf(cmd,"G2\r");
			break;	
		case 2:
			*len = sprintf(cmd,"G3\r");
			break;
		default:
			break;
	}
}


/**********************************************************************************************************
* 函数名称： static int parse_fun(short index, int addr, void *data, char *buf, short len)
* 函数说明： 解析传感器响应的数据，并写入数据库
* 输入参数： 解析索引,传感器地址，变量结构体指针，以及响应数据缓存取指针，响应数据长度
* 返回参数： 返回解析结果，返回0成功，返回1失败
**********************************************************************************************************/
static int parse_fun(short index, int addr, void *data, char *buf, short len)
{
	Data_Struct 	*d = (Data_Struct*)data;
	buf[len] = '\0';
	if(buf!=NULL && buf[0]=='!' && len>0 && buf[len-1]=='\r')
	{	
		switch(index)
		{
			case 0:	//状态信息
				sscanf(buf,"!%d %d %d %f %f %f %f %f\r",&d->batVolt.value.inum,&d->batCapacity.value.inum,&d->batRemainingTime.value.inum,&d->batCurrent.value.fnum,&d->temp.value.fnum,&d->inputFreq.value.fnum,&d->bypassFreq.value.fnum,&d->outputFreq.value.fnum);					
				break;
			case 1:
			//整流器和直流输入状态
				d->rectifierState.value.str 				= buf[2]=='1'?"异常":"正常";			//整流器异常
				Set_Var_Status(&d->rectifierState, 			buf[2]=='1'?Alarm:Normal, NULL);	
				d->batLowVoltProtect.value.str 				= buf[3]=='1'?"发生":"正常";			//电池欠压保护				
				Set_Var_Status(&d->batLowVoltProtect, 		buf[3]=='1'?Alarm:Normal, NULL);	
				d->batLowVolt.value.str 					= buf[4]=='1'?"低压":"正常";			//电池欠压
				Set_Var_Status(&d->batLowVolt, 				buf[4]=='1'?Alarm:Normal, NULL);					
				d->inputOutputType.value.str 				= buf[5]=='1'?"三进单出":"三进三出";	//输入输出类型
				if(d->staticSwitchState.status == Alarm)
				{
					d->runModel.value.str 					= "旁路模式";							//供电模式
					Set_Var_Status(&d->runModel, 			Alarm, NULL);
				}
				else
				{
					d->runModel.value.str 					= buf[6]=='1'?"后备式":"在线式";		//供电模式
					Set_Var_Status(&d->runModel, 			buf[6]=='1'?Alarm:Normal, NULL);
				}
				d->chargeState.value.str 					= buf[7]=='1'?"均充":"浮充";			//电池充电状态
				d->rectifierRunning.value.str				= buf[8]=='1'?"运行":"停止";			//整流器运行状态
				Set_Var_Status(&d->rectifierRunning, 		buf[8]=='1'?Normal:Alarm, NULL);		
			//UPS工作状态
				d->bypassFreqState.value.str				= buf[13]=='1'?"异常":"正常";			//整流器异常
				Set_Var_Status(&d->bypassFreqState, 		buf[13]=='1'?Alarm:Normal, NULL);
				d->manualBypassState.value.str				= buf[14]=='1'?"闭合":"断开";			//手动旁路状态
				Set_Var_Status(&d->manualBypassState, 		buf[14]=='1'?Alarm:Normal, NULL);		
				d->bypassACState.value.str					= buf[15]=='1'?"正常":"异常";			//旁路交流电状态
				Set_Var_Status(&d->bypassACState, 			buf[15]=='1'?Normal:Alarm, NULL);
				d->staticSwitchState.value.str				= buf[16]=='1'?"逆变模式":"旁路模式";	//静态开关状态
				Set_Var_Status(&d->staticSwitchState, 		buf[16]=='1'?Normal:Alarm, NULL);
				d->inverterRunning.value.str				= buf[17]=='1'?"运行":"停止";			//逆变器运行状态
				Set_Var_Status(&d->inverterRunning, 		buf[17]=='1'?Normal:Alarm, NULL);				
			//逆变器故障状态
				d->emergencyStop.value.str					= buf[20]=='1'?"发生":"无";				//紧急停机
				Set_Var_Status(&d->emergencyStop, 			buf[20]=='1'?Alarm:Normal, NULL);			
				d->DCInputHighStop.value.str				= buf[21]=='1'?"发生":"无";				//直流输入过高停机
				Set_Var_Status(&d->DCInputHighStop, 		buf[21]=='1'?Alarm:Normal, NULL);	
				d->manualBypassClose.value.str				= buf[22]=='1'?"发生":"无";				//手动旁路闭合停机
				Set_Var_Status(&d->manualBypassClose, 		buf[22]=='1'?Alarm:Normal, NULL);	
				d->overload.value.str						= buf[23]=='1'?"发生":"无";				//过载停机
				Set_Var_Status(&d->overload, 				buf[23]=='1'?Alarm:Normal, NULL);
				d->outputVoltAbnormal.value.str = 			buf[24]=='1'?"发生":"无";				//逆变器输出电压异常停机
				Set_Var_Status(&d->outputVoltAbnormal, 		buf[24]=='1'?Alarm:Normal, NULL);
				d->overTemp.value.str						= buf[25]=='1'?"发生":"无";				//过温停机
				Set_Var_Status(&d->overTemp, 				buf[25]=='1'?Alarm:Normal, NULL);
				d->outputShortCircuit.value.str				= buf[26]=='1'?"发生":"无";				//输出短路停机
				Set_Var_Status(&d->outputShortCircuit, 		buf[26]=='1'?Alarm:Normal, NULL);
				break;	
			case 2://三相电实时信息参数
				sscanf(buf,"!%f/%f/%f %f/%f/%f %f/%f/%f %f/%f/%f\r",&d->inputUA.value.fnum,&d->inputUB.value.fnum,&d->inputUC.value.fnum,&d->bypassUA.value.fnum,&d->bypassUB.value.fnum,&d->bypassUC.value.fnum,&d->outputUA.value.fnum,&d->outputUB.value.fnum,&d->outputUC.value.fnum,&d->loadPercentageA.value.fnum,&d->loadPercentageB.value.fnum,&d->loadPercentageC.value.fnum);
				break;
				
			default:
				break;
		}
		return 0;
	}
	return 1;
}


/**********************************************************************************************************
* 函数名称： void ups_KR3XXX_Registration(void)
* 函数说明： 科华UPS驱动注册
* 输入参数： 无
* 返回参数： 无
**********************************************************************************************************/
void ups_KR3XXX_Registration(void)
{
	DeviceDriverRegistration(
		"ups",													//设备类型
		"UPS监测",												//设备名称（导航栏默认显示名称）
		"科华",													//设备品牌			
		"KR3XXX",												//设备型号
		"适用于科华KR系列三进三出和三进单出UPS状态监测，协议类型为RS232C通信协议，指令为G1<cr>、G2<cr>、G3<cr>",//驱动描述
		var_attr,												//变量属性声明
		sizeof(Data_Struct),									//变量结构体空间大小
		cmd_fun,												//发送指令生成函数
		parse_fun,												//数据解析函数
		800														//设备响应超时时间		
	);	
}

